package com.darren.euc.service.impl;

import com.darren.euc.entity.ClientParamsEntity;
import com.darren.euc.entity.Member;
import com.darren.euc.repository.ClientParamsEntityRepository;
import com.darren.euc.service.ClientParamsEntityService;
import com.darren.euc.util.Constant;
import org.apache.http.client.utils.DateUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Machenike-Pc on 2018/4/5.
 */
@Service
public class ClientParamsEntityImpl implements ClientParamsEntityService {
    @Autowired
    private ClientParamsEntityRepository clientParamsEntityRepository;

    @Override
    public List<ClientParamsEntity> setJobStartAndEndTime(String startTime, String endTime, Member member) throws Exception {
        if(StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
            throw new Exception("开始时间和结束时间都不可以为空");
        }
        try{
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
         //   Date x= new DateTime(startTime).toDate();
            Date x = formatter.parse(startTime);
            x = formatter.parse(endTime);
        }catch(Exception e){
            throw new Exception("时间格式不对");
        }
        List<ClientParamsEntity> array = new ArrayList<>();
        ClientParamsEntity startJob =this.createClientParamsEntity(member,Constant.CLIENT_JOB_START_TIME,startTime);
        ClientParamsEntity endJob = this.createClientParamsEntity(member,Constant.CLIENT_JOB_END_TIME,endTime);
        clientParamsEntityRepository.save(startJob);
        array.add(startJob);
        clientParamsEntityRepository.save(endJob);
        array.add(endJob);
        return array;
    }

    private ClientParamsEntity createClientParamsEntity(Member member, String kind, String time) throws Exception {

        ClientParamsEntity db = clientParamsEntityRepository.findByClientIdAndKey(member.getClientId(),kind);
        if(db != null){
            db.setLastModifyTime(new Date());
            db.setLastModifyBy(member.getName());
            db.setValue(time);
            return db;
        }
        ClientParamsEntity clientParamsEntity = new ClientParamsEntity();
        clientParamsEntity.setKey(kind);
        clientParamsEntity.setValue(time);
        if(member.getClientId() == null){
            throw new Exception("会员对应的客户信息为空");
        }
        if(member.getSpaceId() == null){
            throw new Exception("会员对应的空间信息为空");
        }
        clientParamsEntity.setSpaceId(member.getSpaceId());
        clientParamsEntity.setClientId(member.getClientId());

        clientParamsEntity.setCreatedBy(member.getName());
        clientParamsEntity.setCreateTime(new Date());
        clientParamsEntity.setLastModifyTime(new Date());
        return clientParamsEntity;
    }

    @Override
    public Boolean judgeInJobTime(String time, Integer clientId) throws Exception {
        ClientParamsEntity start = clientParamsEntityRepository.findByClientIdAndKey(clientId,Constant.CLIENT_JOB_START_TIME);
        ClientParamsEntity end = clientParamsEntityRepository.findByClientIdAndKey(clientId,Constant.CLIENT_JOB_END_TIME);
        if(start == null || end ==null)
            return false;//没有设置就算过
        try{
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

//            Date x= new DateTime(time).toDate();
            Date x = formatter.parse(time);
        return judgeTimeIn(formatter.parse(start.getValue()),formatter.parse(end.getValue()),x);
        }catch(Exception e){
            throw new Exception("时间格式不对");
        }
    }

    @Override
    public List<ClientParamsEntity> findJobTimeByClientId(int clientId) {
        ClientParamsEntity start = clientParamsEntityRepository.findByClientIdAndKey(clientId,Constant.CLIENT_JOB_START_TIME);
        ClientParamsEntity end = clientParamsEntityRepository.findByClientIdAndKey(clientId,Constant.CLIENT_JOB_END_TIME);
        if(start == null || end ==null)
            return null;
        List<ClientParamsEntity> array = new ArrayList<>();
        array.add(start);
        array.add(end);
        return array;
    }

    private Boolean judgeTimeIn(Date start, Date end, Date x) {
        return x.getTime()-start.getTime()>0 && end.getTime()-x.getTime()>0;
    }
}
